package source.events;

import source.Driver;
import source.view.MainScreen;
/**
 * Class Turn is responsible for keeping track of what player currently has control
 * on the game.  The turn object has one method, nextTurn().  This method increments
 * the index on the current player, tells the view to change to the appropriate
 * PlayerKeyListener, and tells the current player himself to execute his own new turn
 * method.
 * @author Joe
 *
 */
public class Turn implements EventListener<ModelEvent>{

	private static final int numPlayers = Driver.numPlayers;
	private int currentPlayer;
	private EventHandler<ViewEvent> veh;
	private EventHandler<ModelEvent> meh;
	
	public Turn(EventHandler<ViewEvent> veh, EventHandler<ModelEvent> meh)
	{
		currentPlayer = 0;
		this.meh = meh;
		this.veh = veh;
	}
	
	public void nextTurn(int currentPlayer)
	{
		//currentPlayer = (currentPlayer + 1)%numPlayers;
		//switch to the current PlayerKeyListener

		
		/*TODO:  figure out why fireEventNow is needed instead of fireEvent.  If fireEvent
		* is called, then the ViewEvent some how changes it's type to NEXTTURN.
		*/
		veh.fireEventNow(new ViewEvent(currentPlayer, ViewEventType.NEWTURN));
		//call the next turn methods of model
		meh.fireEventNow(new ModelEvent(currentPlayer, ModelEventType.NEXTTURN));
	}	
	
	public boolean eventCallback(ModelEvent e)
	{
		switch(ModelEventType.valueOf(e.getType()))
		{
			case ENDTURN:
			nextTurn(e.getPlayerID());
			return true;
		}
		return false;
	}
}
